package com.test.httpurlconnectiondemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.test.httpurlconnectiondemo.listener.HttpCallbackListener;
import com.test.httpurlconnectiondemo.utils.HttpUtil;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        Button mButton = findViewById(R.id.mButton);
        mTextView = findViewById(R.id.mTextView);

        mButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.mButton:
                String httpUrl = "https://www.baidu.com/";
                HttpUtil.getSendHttpRequest(httpUrl, new HttpCallbackListener() {
                    @Override
                    public void onFinish(final String response) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mTextView.setText(response);
                            }
                        });
                    }

                    @Override
                    public void onError(final Exception e) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                e.printStackTrace();
                            }
                        });
                    }
                });
                break;
        }
    }
}
